﻿/*
    创 建 人     ： 陈 锐
    创 建 时 间  ： 2012.01.17
    参 数 说 明  ： 
        PreBtnId ： 向前翻页按钮ID
        PreBtnId ： 向前翻页按钮ID
        PageSize :  每次滑动项数
        Speed    ： 完成滑动时间
    参 数 形 式  ：
        $("#box").daanSlider({PreBtnId ：....[,PageSize:...]})
*/
(function () {
    $.extend($.fn, {
        daanSlider: function (option) {
            var setting = $.extend({
                PreBtnId: "#prepage",
                NextBtnId: "#nextpage",
                PageSize: -1,
                Speed: 1000,
                PreSliding: function () { },
                SlidingCompleted: function () { }
            }, option);

            var $mainBox = this;
            var sliding = false;
            var allowPref = true;
            var allowNext = true;
            $mainBox.css("overflow", "hidden");

            var $ul = $mainBox.find("ul:first");
            var items = $ul.find("li");

            var itemWidth = items.first().outerWidth();
            var itemHeight = items.first().outerHeight();
            var marginLeft = items.first().css("margin-left").replace("px", "");
            var marginRight = items.first().css("margin-right").replace("px", "");

            itemWidth = itemWidth + Number(marginLeft) + Number(marginRight);

            $ul.css({ "width": (items.length * itemWidth) + "px", "height": itemHeight + "px", "overflow": "hidden" });
            $mainBox.css("height", itemHeight + "px");

            var pageItemsCount = Math.round($mainBox.innerWidth() / itemWidth);

            pageItemsCount = setting.PageSize > 0 ? setting.PageSize : pageItemsCount;

            var pageCount = Math.ceil(items.length / pageItemsCount);

            var pageIndex = 0;
            var me = this;

            $ul.css({ "width": (pageCount * itemWidth * pageItemsCount) + "px" });



            var slideToPageIndex = function () {
                if (setting.PreSliding && typeof setting.PreSliding == "function") {
                    setting.PreSliding.call(me);
                }
                $mainBox.animate({ scrollLeft: itemWidth * pageIndex * pageItemsCount }, setting.Speed, function () {
                    sliding = false;
                    if (setting.SlidingCompleted && typeof setting.SlidingCompleted == "function") {
                        setting.SlidingCompleted.call(me);
                    }
                });
            }

            var slideToPrePage = function () {
                if (pageIndex > 0) {
                    pageIndex--;
                }
                else {
                    pageIndex = pageCount - 1;
                }
                slideToPageIndex();
            }

            var slideToNextPage = function () {
                if (pageIndex < pageCount - 1) {
                    pageIndex++;
                }
                else {
                    pageIndex = 0;
                }
                slideToPageIndex();
            }

            var prePage = function () {
                if (sliding) return;
                if (!allowPref) return;
                sliding = true;
                slideToPrePage();
            }

            var nextPage = function () {
                if (sliding) return;
                if (!allowNext) return;
                sliding = true;
                slideToNextPage();
            }


            $(setting.PreBtnId).bind("click", function () {
                prePage();
            });

            $(setting.NextBtnId).bind("click", function () {
                nextPage();
            });

            this.PrePage = function () {
                prePage();
            }

            this.NextPage = function () {
                nextPage();
            }

            this.SetPreEnable = function (enablePre) {
                allowPref = enablePre;
            };

            this.SetNextEnable = function (enableNext) {
                allowNext = enableNext;
            };

            this.PageIndex = function () {
                return pageIndex;
            };
            this.PageCount = function () {
                return pageCount;
            };

            return this;
        }
    });
})();